<?php
namespace app\api\controller\user;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\company\Contract;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\buss\Buss;
use app\common\model\power\Rolepowerlnk;
use app\api\controller\Base;
use app\common\model\power\Rolepower;
use app\common\model\customer\Customer as Customermodel;
use app\common\model\orderhawa\HawaList;
class Regulatory extends Base
{
	/**
	 * 产生jwt加密串，参考session
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(207001);
	}
	
	/**
	 * 获取客户（非产废单位）基础信息
	 * @return [type] [description]
	 */
	public function getcustomerinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = Customermodel::user_getinfo($id,'regulatory');
		if(empty($data)){
			return myerr('没有找到此客户');
		}
		return myok($data);
	}

	/**
	 * 获取已在平台的产废单位基础信息
	 * @return [type] [description]
	 */
	public function getcompanyinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = Company::user_getinfo($id);
		if(empty($data)){
			return myerr('没有找到此单位');
		}
		$rdata['companyinfo'] = $data;
		$sdata['Type'] = 'contract';
		$sdata['where'] = [['CompanyId',$id]];

		$rdata['contractlist'] =  Contract::usergetlist($sdata,'regulatory',false);
		$sdata['Type'] = 'hawa';
		$sdata['where'] = [['CompanyId',$id]];
		$sdata['order'] = 'UpTime desc';
		$sdata['limit'] = '10';

		$rdata['hawaorder'] = HawaList::usergetlist($sdata,'regulatory',false);
		$rdata['userinfo'] = UserModel::getinfo('',$data['UserId']);
		$rdata['StateText'] = Company::getStatusText();
		$rdata['power'] = Rolepowerlnk::getpowerlnk('company');
		return myok($rdata);
	}
	
	public function maplist(){
		$key = trim(input('searchkey'));
		if($key=="%"){return moerr('非法参数');}
		$idata = input('post.');
		//定位
		if(!empty($idata['lat'])){
			
			$sdata['field'] = [str_distance($idata['lat'],$idata['lon'])];
			$sdata['order'] = 'distance';
		}
		
		//没有定位直接显示关键词
		if(!empty($key)){
			$sdata = ['where'=>[['Name','like','%'.$key.'%']]];
		}

		if(empty($idata['limit'])){
			$sdata['limit'] = '99';
		}else if($idata['limit']!=='-1'){
			$sdata['limit'] = $idata['limit'];
		}

		$sdata['where'][]=['Lat','not null'];
		$sdata['Type'] = 'company';
		$data = Company::usergetlist($sdata,'regulatory',false);
		if(!empty($idata['showpointicon'])){
			$sdata['Type'] = 'customer';
			$sdata['Where'][] = ['CompanyId','null'];
			$data['custlist'] = Customermodel::usergetlist($sdata,'buss',false);
		}
		return myok($data);	
	}

	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
